<!-- src/views/Home.vue -->
<script setup>
import { onMounted, reactive,ref} from 'vue';
import { queryUserWorkorderList,getUserStatistics } from '@/apis/workorderAPI';
import { ElMessage } from 'element-plus';
import { priorityMap } from '@/utils/dict';
const statistics = ref({});
const currentPage = ref(1);      // 当前页码
const pageSize = ref(1000);
const params=reactive({
  pageNo:'',
  pageSize:'',
  status:1
})
const userWorkorderList=ref([]);
onMounted(async()=>{
  await fetchData();
  await getStatistics();
})
const fetchData = async () => {
  try {
    params.pageNo = currentPage.value;
    params.pageSize = pageSize.value;
    const { data } = await queryUserWorkorderList(params);
    const { records } = data;
    userWorkorderList.value = records;

  } catch (error) {
    console.error('错误:', error);
    ElMessage.error("加载失败");
  }
};
const getStatistics=async()=>{
  try{
    const{data}=await getUserStatistics();
    statistics.value=data;
  }catch{
    ElMessage.error("加载失败");
  }
}
</script>

<template>
  <div class="dashboard">
    <h2 class="section-title">待办事项 </h2>
    <div class="todo-cards">
      <div class="card todo-card">所有工单 <span class="count">{{ statistics.totalOrderCount }}</span></div>
       <div class="card todo-card">今日工单 <span class="count">{{ statistics.todayOrderCount }}</span></div>
      <div class="card todo-card">本月收入 <span class="count">{{ statistics.monthlyIncome }}</span></div>
      <div class="card todo-card">总收入 <span class="count">{{ statistics.totalIncome }}</span></div>
    </div>
  </div>
  <h2 class="section-title">进行中工单</h2>
  <div class="IPWorkorder-table">
    <el-table :data="userWorkorderList" label-width="80px">
      <el-table-column label="标题" prop="title"/>
      <el-table-column label="姓名" prop="name"/>
      <el-table-column label="电话" prop="phone"/>
      <el-table-column label="地址" prop="address"/>
      <el-table-column label="优先级" prop="priorityType">
        <template #default="scope">
          <el-tag type="primary"  class="large-tag" v-if="scope.row.priorityType===1">{{ priorityMap[scope.row.priorityType] ?? '未知'}}</el-tag>
          <el-tag type="danger"  class="large-tag" v-if="scope.row.priorityType===2">{{ priorityMap[scope.row.priorityType] ?? '未知'}}</el-tag>
          <el-tag type="error"  class="large-tag" v-if="scope.row.priorityType===3">{{ priorityMap[scope.row.priorityType] ?? '未知'}}</el-tag>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<style scoped lang="scss">
/* 全局样式 */
* {
  margin: 0;

  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
/* 仪表盘内容 */
.dashboard {
  padding: 24px;
}

.section-title {
  font-size: 18px;
  color: #333;
  margin-bottom: 16px;
  font-weight: 600;
  display: flex;
  align-items: center;
  margin-left: 25px;
}

.section-title::after {
  content: '';
  flex: 1;
  height: 1px;
  background-color: #eee;
  margin-left: 16px;
}
.IPWorkorder-table{
  margin-left: 25px;
  margin-right: 25px;
}

/* 卡片容器 */
.todo-cards, .data-cards {
  display: flex;
  gap: 16px;
  margin-bottom: 24px;
  flex-wrap: wrap;
}

/* 卡片样式 */
.card {
  flex: 1;
  min-width: 160px;
  padding: 20px;
  border-radius: 8px;
  background-color: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  font-weight: 500;
  color: #555;
}

.card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 4px;
  height: 100%;
}

.todo-card {
  border: 1px solid #f1c40f;
  cursor: pointer;
}

.todo-card::before {
  background-color: #f1c40f;
}

.todo-card:hover {
  transform: translateY(-5px) scale(1.02);
  box-shadow: 0 10px 20px rgba(241, 196, 15, 0.2);
}

.data-card {
  border: 1px solid #3498db;
}

.data-card::before {
  background-color: #3498db;
}
.data-card:hover {
  transform: translateY(-5px) scale(1.02);
  box-shadow: 0 10px 20px rgba(15, 38, 241, 0.2);
}

.count {
  display: block;
  font-size: 24px;
  font-weight: 700;
  margin-top: 8px;
}

.todo-card .count {
  color: #f39c12;
}

.data-card .count {
  color: #2980b9;
}

.large-tag {
  font-size: 14px;
  padding: 10px 10px;
  height: 12px;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .chart-container {
    flex-direction: column;
  }
}
</style>
